################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Analysis ESS
#Figure 1
#
#Verena Fetscher
#July 2022
####################


####################
# Load data
####################
source("dataframes.R")

##########################
#Redistribution preferences
##########################

#Average preferences for redistribution (above mean earners)
#Produces information on redistribution preferences (in percentages) in main text.
length(data_imputed$redistribution[data_imputed$income.dist.th>0])
prefs<-100*prop.table(table(data_imputed$redistribution[data_imputed$income.dist.th>0]))

rownames(prefs)<-c("Disagree strongly","Disagree","Neither",
                   "Agree","Agree strongly")

xtable(t(prefs), caption="Preferences for redistribution (in %)")



##########################
#Produces Figure 1: Support for redistribution across Western European countries (above mean earners, 2002-2014).
##########################

data_imputed%>%
  group_by(country,redistribution)%>%
  filter(income.dist.th>0)%>%
  select(redistribution)%>%
  summarise (n = n()) %>%
  mutate(freq = n / sum(n))->data01

data01%>%
  group_by(country)%>%
  mutate(high=(freq[redistribution==5]+freq[redistribution==4]),
         low=(freq[redistribution==1]+freq[redistribution==2]),
         neither=freq[redistribution==3])%>%
  gather(support, value, c(high,low,neither))->data01

data01$support[data01$support=="high"]<-3
data01$support[data01$support=="neither"]<-2
data01$support[data01$support=="low"]<-1

data01%>%
  select(country, support, value)%>%
  arrange(desc(support),desc(value))%>% 
  distinct(value,support)->data01

data01$country <- factor(data01$country, 
                         levels = unique(data01$country))
data01%>%
  ggplot(aes(fill=factor(support),y=value,x=country, 
             label = paste0(round(value,2)*100,"%")), size=4)+
  geom_bar(stat = "identity")+
  geom_text(size = 3, position = position_stack(vjust = 0.5))+
  coord_flip()+
  ylab("Redistribution preferences\n in Western European countries (2002-2014).")+
  theme_bw()+
  theme(panel.border=element_blank(),axis.line=element_line(),
        legend.position="bottom",legend.text = element_text(size = 14),
        legend.title = element_text(size=14),axis.text=element_text(size=12),
        axis.title=element_text(size=14),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.y = element_blank())+
  scale_fill_brewer(palette="Greys",
                    name=element_blank(),
                    labels=c("Oppose","Neither nor","Support"),
                    guide = guide_legend(reverse = TRUE))



#Compare with listwise deletion
prefs_list<-100*prop.table(table(data_list$redistribution[data_list$income.dist.th>0]))

rownames(prefs_list)<-c("Disagree strongly","Disagree","Neither",
                        "Agree","Agree strongly")

xtable(t(prefs_list), caption="Preferences for redistribution (in %)")


##########################
#Save figure
##########################

ggsave(file="figure_1.pdf", height = 5.83, width = 8.27, units = "in")

